package com.daye.homework.dao.impl;

import com.daye.homework.bean.Student;
import com.daye.homework.dao.AdminDao;
import com.daye.homework.util.DruidUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl implements AdminDao {
    /**
     * 管理员登录, 固定为admin和123456
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public boolean findAdmin(String username, String password) {
        if ("admin".equals(username) && "123456".equals(password)){
            return true;
        }
        return false;
    }

    /**
     * 添加学生
     *
     * @param student
     */
    @Override
    public void insertStudent(Student student) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "insert into t_student values( ? , ? , ? , ? , ? , ? , ? )";

        Object[] parma = {null , student.getStudentNum() , student.getName() , student.getSex() , student.getBirthday() , student.getEmail() , student.getDesc()};

        // int i = qr.update(sql, new BeanHandler<Student>(Student.class) , student);
        int i = qr.update(sql, parma);
    }

    /**
     * 遍历所有学生
     *
     * @return
     */
    @Override
    public List<Student> selectAllStudent() throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select  * from t_student";
        List<Student> studentList = qr.query(sql, new BeanListHandler<Student>(Student.class));
        return studentList;
    }

    /**
     * 根据id删除学生
     *
     * @param id
     */
    @Override
    public void removeStudentById(Integer id) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "delete from t_student where id = ?";

        qr.update(sql , id);
    }

    /**
     * 根据学员id找到学员
     *
     * @param id
     * @return
     */
    @Override
    public Student selectStudentById(int id) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "select * from t_student where id = ?";
        Student query = qr.query(sql, new BeanHandler<Student>(Student.class), id);

        return query;
    }

    /**
     * 根据id修改学员信息
     *
     * @param student
     */
    @Override
    public void updateStudentById(Student student) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "UPDATE t_student SET studentNum=? , name = ? , sex = ? , birthday = ? , email = ? , `desc` = ? WHERE id = ?";

        Object[] parma = {student.getStudentNum() , student.getName() , student.getSex() , student.getBirthday() , student.getEmail() , student.getDesc() , student.getId()};

        qr.update(sql , parma);
    }
}
